package com.liuhengjia.aiknowledgebase.api;

import com.liuhengjia.aiknowledgebase.advisor.ChatMessageAdvisor;
import com.liuhengjia.aiknowledgebase.common.RestResult;
import com.liuhengjia.aiknowledgebase.service.ChatService;
import com.liuhengjia.aiknowledgebase.api.response.ConversationVO;

import java.util.List;

import lombok.RequiredArgsConstructor;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 聊天接口
 */
@RestController
@RequestMapping("/api/conversation")
@RequiredArgsConstructor
public class ChatAPI {
    private final ChatClient chatClient;
    private final ChatMemory chatMemory;
    private final ChatService chatService;

    /**
     * 新增会话
     *
     * @return RestResult<ConversationVO>
     */
    @PostMapping("/create")
    public RestResult<ConversationVO> create() {
        return RestResult.buildSuccessResult(chatService.create());
    }

    /**
     * 编辑会话
     *
     * @param conversationId 会话 ID
     * @param title          会话标题
     * @return RestResult<Void>
     */
    @PutMapping("/edit")
    public RestResult<Void> edit(
            @RequestParam(value = "conversationId") String conversationId,
            @RequestParam(value = "title") String title) {
        chatService.edit(conversationId, title);
        return RestResult.buildSuccessResult();
    }

    /**
     * AI 聊天
     *
     * @param conversationId 会话 ID
     * @param message        问题信息
     * @return 模型应答
     */
    @GetMapping("/chat")
    public RestResult<String> chat(@RequestParam(value = "conversationId") String conversationId,
                                   @RequestParam(value = "message") String message) {
        return RestResult.buildSuccessResult(chatClient.prompt()
                .user(message)
                .advisors(new ChatMessageAdvisor(chatMemory, conversationId))
                .call()
                .content());
    }

    /**
     * 删除会话
     *
     * @param conversationId 会话 ID
     * @return 操作结果
     */
    @DeleteMapping("/del")
    public RestResult<Void> del(@RequestParam(value = "conversationId") String conversationId) {
        chatService.delete(conversationId);
        return RestResult.buildSuccessResult();
    }

    /**
     * 获取会话列表
     *
     * @return RestResult<List < ConversationVO>>
     */
    @GetMapping("/list")
    public RestResult<List<ConversationVO>> list() {
        return RestResult.buildSuccessResult(chatService.list());
    }

    /**
     * 会话 ID 获取会话
     *
     * @param conversationId 会话 ID
     * @return RestResult<ConversationVO>
     */
    @GetMapping("/get")
    public RestResult<ConversationVO> get(@RequestParam(value = "conversationId") String conversationId) {
        return RestResult.buildSuccessResult(chatService.get(conversationId));
    }
}
